package com.zzw.testdemo01.item;

import com.zzw.testdemo01.itemGroup.ZzwItemGroup;
import com.zzw.testdemo01.registry.ModGroup;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.effect.LightningBoltEntity;
import net.minecraft.item.*;
import net.minecraft.potion.EffectInstance;
import net.minecraft.potion.Effects;
import net.minecraft.world.World;
import net.minecraft.world.server.ServerWorld;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ItemOne extends SwordItem {
    private static final Logger log = LoggerFactory.getLogger(ItemOne.class);

    public ItemOne() {
        super(ItemTier.DIAMOND, 8, -2.4F, new Item.Properties().tab(ModGroup.ZZW_GROUP));
        // 8是攻击伤害，-2.4F是攻击速度
    }


    /**
     * 是否对敌人造成伤害，并施加虚弱II效果，被雷劈
     * @param itemStack 使用的物品堆栈
     * @param attacker 受害者实体
     * @param victim 攻击者实体
     * @return 是否成功伤害敌人
     */
    @Override
    public boolean hurtEnemy(ItemStack itemStack, LivingEntity attacker, LivingEntity victim) {
        boolean alive = attacker.isAlive();
        log.info("alive:  {}", alive);
        if (!alive){
            this.setDamage(itemStack, itemStack.getDamageValue()+1);
            log.info("damage:  {}", itemStack.getDamageValue());
        }
        boolean isHurt = super.hurtEnemy(itemStack, attacker, victim);
        if (isHurt){
            victim.addEffect(new EffectInstance(Effects.WEAKNESS, 100, 1));
            // 1级虚弱效果持续
            // 100ticks即5秒
        }
        // 使用Forge API尝试在被攻击生物位置生成雷击
        if (!attacker.level.isClientSide()) {
            ServerWorld serverWorld = (ServerWorld) attacker.level;
            LightningBoltEntity lightningBolt = new LightningBoltEntity(EntityType.LIGHTNING_BOLT, serverWorld);
            lightningBolt.setPos(attacker.getX(), attacker.getY(), attacker.getZ());
            lightningBolt.setVisualOnly(false); // 确保雷击具有实际效果
            serverWorld.addFreshEntity(lightningBolt);
        }
        return isHurt;
    }



}
